package com.egl.web;

public class WebServerFactory {

	private WebServerFactory() {
	}

	public static WebServer createWebServer() {
		return createWebServer(DefaultWebServer.class);
	}

	public static WebServer createWebServer(String className)
			throws ClassNotFoundException {
		Class<?> clz = null;
		clz = Class.forName(className);
		return createWebServer(clz);
	}

	public static WebServer createDefaultWebServerIfFails(String className) {
		Class<?> clz = null;
		try {
			clz = Class.forName(className);
		} catch (ClassNotFoundException e) {
			clz = DefaultWebServer.class;
		}
		return createWebServer(clz);
	}

	public static WebServer createWebServer(Class<?> clz) {
		WebServer webServer = null;
		Object obj = null;
		try {
			obj = clz.newInstance();
		} catch (InstantiationException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
		if (obj instanceof WebServer) {
			webServer = (WebServer) obj;
		} else if (obj != null) {
			throw new RuntimeException(obj.getClass()
					+ " is not instaceof com.egl.web.WebServer");
		} else {
			throw new RuntimeException("null object");
		}
		return webServer;
	}
}
